#!/usr/bin/env python3

import os
import sys
import subprocess

def run(cmd, **kwargs):
    process = subprocess.Popen(cmd,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               encoding='gbk' if sys.platform =="win32" else 'utf-8',
                               **kwargs)
    for line in iter(process.stdout.readline, ''):
        if line:
            print(line)

    process.wait(timeout=3)
    ret_code = process.returncode
    return ret_code

if __name__ == "__main__":
    root = os.path.dirname(os.path.abspath(__file__))
    bin = sys.argv[1]
    path = os.path.join(root, sys.argv[2])
    if sys.platform == "win32":
        prefix = ["cmd", "/c"]
    else:
        prefix = ["sh", "-c"]

    cmd = [*prefix, f'"{path}/{bin}"']

    run(cmd, cwd=path)
